﻿using DotNetClient;
using DoudizhuMsg;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace Doudizhu
{
    public class PlayerEntity
    {
        public GameObject go;
        public Player player;

        private Image _avata;
        private Text _name;
        private Text _gold;
        private Text _total;
        private Text _win;
        private Text _winRate;
        private GameObject _ready;

        public PlayerEntity(GameObject go)
        {
            this.go = go;

            _avata = go.transform.Find("Avata/Avata_img").GetComponent<Image>();
            _name = go.transform.Find("Name_txt").GetComponent<Text>();
            _gold = go.transform.Find("Gold/Gold_txt").GetComponent<Text>();
            _total = go.transform.Find("Total/Total_txt").GetComponent<Text>();
            _win = go.transform.Find("Win/Win_txt").GetComponent<Text>();
            _winRate = go.transform.Find("WinRate/WinRate_txt").GetComponent<Text>();
            _ready = go.transform.Find("Ready_go").gameObject;
        }

        public void Update(Player player)
        {
            this.player = player;

            uint total = player.Doudizhu.Win + player.Doudizhu.Lose + player.Doudizhu.Escape;
            float winrate = player.Doudizhu.Win > 0 ? (float)total / player.Doudizhu.Win : 0;

            _name.text = player.Nickname;
            _gold.text = player.Doudizhu.Gold.ToString();
            _total.text = total.ToString();
            _win.text = player.Doudizhu.Win.ToString();
            _winRate.text = winrate.ToString("0%");
            _ready.SetActive(player.State == (int)PLAYER_STATE.READY);

            if (player.Uid == MyselManager.Instance.Myself.Id) _avata.color = Color.green;
            else _avata.color = Color.white;
        }

        public void UpdateState(int state)
        {
            player.State = state;
            _ready.SetActive(player.State == (int)PLAYER_STATE.READY);
        }
    }

    public class GamePlayerEntity
    {
        public GameObject go;
        public Player player;
        public GamePlayer gamePlayer;

        private Image _avata;
        private Text _name;
        private Text _level;
        private Text _role;

        public GamePlayerEntity(GameObject go)
        {
            this.go = go;

            _avata = go.transform.Find("Avata/Avata_img").GetComponent<Image>();
            _name = go.transform.Find("Name_txt").GetComponent<Text>();
            _level = go.transform.Find("Level_txt").GetComponent<Text>();
            _role = go.transform.Find("Role_txt").GetComponent<Text>();
        }

        public void Update(Player player, GamePlayer gamePlayer)
        {
            this.player = player;
            this.gamePlayer = gamePlayer;

            _name.text = player.Nickname;
            _level.text = getLevel(player.Doudizhu.Gold);
            _role.text = getRole(gamePlayer.Role);
        }

        public void UpdateSeat(Transform tr)
        {
            go.transform.SetParent(tr, false);
            go.transform.localPosition = Vector3.zero;
        }

        public void UpdateRole(int role)
        {
            gamePlayer.Role = role;
            _role.text = getRole(gamePlayer.Role);
        }

        private string getLevel(int glod)
        {
            if (glod <= 10) return "包身工";
            else if (glod <= 25) return "短工";
            else if (glod <= 40) return "长工";
            else if (glod <= 80) return "佃户";
            else if (glod <= 140) return "贫农";
            else if (glod <= 230) return "渔夫";
            else if (glod <= 365) return "猎人";
            else if (glod <= 500) return "中农";
            else if (glod <= 700) return "富农";
            else if (glod <= 1000) return "掌柜";
            else if (glod <= 1500) return "商人";
            else if (glod <= 2200) return "衙役";
            else if (glod <= 3000) return "小财主";
            else if (glod <= 4000) return "大财主";
            else if (glod <= 5000) return "小地主";
            else if (glod <= 7700) return "大地主";
            else if (glod <= 10000) return "知县";
            else if (glod <= 14000) return "通判";
            else if (glod <= 20000) return "知府";
            else if (glod <= 30000) return "总督";
            else if (glod <= 45000) return "巡抚";
            else if (glod <= 70000) return "丞相";
            else return "帝王";
        }

        private string getRole(int role)
        {
            if (role == 1) return "农民";
            else if (role == 2) return "地主";
            else return "";
        }
    }

    public class CardEntity : MonoBehaviour, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler
    {
        public CardAssets cardAssets;

        public Card card { get; private set; }

        private Image _color_center;
        private Image _color_left;
        private Image _color_right;
        private Image _score_left;
        private Image _score_right;
        private Image _joker_left;
        private Image _joker_right;

        private void Awake()
        {
            _color_center = transform.Find("color_center").GetComponent<Image>();
            _color_left = transform.Find("color_left").GetComponent<Image>();
            _color_right = transform.Find("color_right").GetComponent<Image>();
            _score_left = transform.Find("score_left").GetComponent<Image>();
            _score_right = transform.Find("score_right").GetComponent<Image>();
            _joker_left = transform.Find("joker_left").GetComponent<Image>();
            _joker_right = transform.Find("joker_right").GetComponent<Image>();
        }

        public void Init(Card card)
        {
            this.card = card;

            if (card.Score == (int)SCORE.joker_black || card.Score == (int)SCORE.joker_red)
            {

            }
            else
            {

            }
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            throw new System.NotImplementedException();
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            throw new System.NotImplementedException();
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            throw new System.NotImplementedException();
        }


    }

}
